package batch_kube

import (
	"context"
	"fmt"
	"golang_study/internal/util"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
)

type delConfigmapFun func(string, string, *kubernetes.Clientset)

func GetConfigmap(ns string, clientSet *kubernetes.Clientset) []string {
	var configmap []string
	c, _ := clientSet.CoreV1().ConfigMaps(ns).List(context.Background(), metav1.ListOptions{})

	for n, item := range c.Items {
		configmap = append(configmap, item.Name)
		fmt.Printf("Configmap: %d %s %s\n", n+1, item.Namespace, item.Name)
	}
	fmt.Println()

	return configmap
}

func delConfigmap(ns, name string, clientSet *kubernetes.Clientset) {
	err := clientSet.CoreV1().ConfigMaps(ns).Delete(context.Background(), name, metav1.DeleteOptions{})

	if err != nil {
		fmt.Printf("Error deleting configmap, %s\n", name)
	}
}

func DelConfigmaps(ns string, names []string, interactive bool, clientSet *kubernetes.Clientset) {
	if interactive {
		interactiveResource(ns, delConfigmap, clientSet)
	} else {
		if names != nil {
			for _, n := range names {
				delConfigmap(ns, n, clientSet)
			}
		} else {
			util.IsBatch(ns)

			pods := GetConfigmap(ns, clientSet)
			for _, p := range pods {
				delConfigmap(ns, p, clientSet)
			}
		}
	}
	//if cm != nil {
	//	for _, c := range cm {
	//		delConfigmap(ns, c, clientSet)
	//	}
	//} else {
	//	util.IsBatch(ns)
	//
	//	configmap := GetConfigmap(ns, clientSet)
	//	for _, cm := range configmap {
	//		delConfigmap(ns, cm, clientSet)
	//	}
	//}
}
